import {JeesdpHttp as axios} from "@jeesdp/components";
import cookie from 'js-cookie';

export default {
    logout() {
        cookie.remove("token_type");
        cookie.remove("access_token");
        cookie.remove("refresh_token");
        localStorage.removeItem("userinfo");
        localStorage.removeItem("defaultActive");
        window.location.href = '/oauth2/logout';
    },
    token(params) {
        return axios.post(`${import.meta.env.VITE_AUTH_URL}/oauth2/token`, {
            code: params.code,
            client_id: import.meta.env.VITE_CLIENT_ID,
            client_secret: import.meta.env.VITE_CLIENT_SECRET,
            grant_type: import.meta.env.VITE_GRANT_TYPE,
            redirect_uri: import.meta.env.VITE_API_URL
        }, {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            }
        });
    },
    refresh() {
        axios.post('/oauth2/token', {
            grant_type: 'refresh_token',
            refresh_token: cookie.get('refresh_token'),
            client_id: import.meta.env.VITE_CLIENT_ID,
            client_secret: import.meta.env.VITE_CLIENT_SECRET,
        }, {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            }
        }).then((response) => {
            console.log(response);
        })
    },
    userinfo() {
        return axios.post(`/oauth2/userinfo`);
    }
}
